﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ToolUtil
{
    public class MD5
    {
        public static string Encrypt(string s)
        {
            s = s.Replace("\0", "");

            const int len = 5;
            byte[] bytes = Encoding.Default.GetBytes(s);
            int[] data = new int[10];
            for (int i = 0; i < len; i++)
            {
                int num2 = (bytes[(i * 2)] - 0x80) + (i + 1) * (i + 1);
                int num3 = bytes[(i * 2) + 1] - num2;
                data[i * 2] = num2;
                data[i * 2 + 1] = num3;
                Console.WriteLine(num2 +"::"+ num3);
            }
            byte[] byteArr = data.Select(i => (byte)i).ToArray();
            for(int i = 0; i < byteArr.Length; i++)
            {
                Console.WriteLine(byteArr[i] + "");
            }
            Console.WriteLine(Encoding.Default.GetString(byteArr));
            return Encoding.ASCII.GetString(byteArr);
        }


        /// <summary>
        /// 用户密码解密
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string decrypt(string s)
        {
            byte[] bytes = Encoding.Default.GetBytes(s);
            string str = "";
            string str2 = "";
            if (bytes.Length < 10)
            {
                return Encoding.ASCII.GetString(bytes);
            }
            for (int i = 5; i >= 1; i--)
            {
                int num2 = (bytes[(i * 2) - 2] - 0x80) + (i * i);
                str = Encoding.ASCII.GetString(BitConverter.GetBytes(num2)) + str;
                int num3 = bytes[(i * 2) - 1] - num2;
                str2 = Encoding.ASCII.GetString(BitConverter.GetBytes(num3)) + str2;
            }
            s = str + str2;
            s = s.Replace("\0", "");
            return s.Trim();
        }
    }
}
